package com.xia.controller;

import com.xia.domian.Article;
import com.xia.service.IAttentionService;
import com.xia.util.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 关注系统控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/attention")
public class AttentionController {
    private IAttentionService attentionService;

    @Autowired
    public void setAttentionService(IAttentionService attentionService) {
        this.attentionService = attentionService;
    }

    @GetMapping("/selectMsgAttentionGet/{usersId}/{loginUsersId}")
    public Result selectMsgAttentionGet(@PathVariable Integer usersId, @PathVariable Integer loginUsersId){
        return Result.success(attentionService.selectMsgAttention(usersId,loginUsersId));
    }

    /**
     * 查看用户动态
     */
    @GetMapping("/selectUsersMsgDynamic/{usersId}/{index}")
    public Result selectUsersMsgDynamic(@PathVariable Integer usersId,@PathVariable Integer index){
        List<Integer> id = new ArrayList<>();
        id.add(usersId);
        List<Article> sortArticleList = attentionService.selectDynamicAllUtil(id,index);
        if (sortArticleList == null){
            return Result.error("没有动态");
        }
        return Result.success(sortArticleList);
    }

    /**
     * 查询用户在创作中心的信息
     */
    @GetMapping("/selectUsersCreatorData/{usersId}")
    public Result selectUsersCreatorData(@PathVariable Integer usersId){
        Map<String,Object> map=attentionService.selectUsersInCreator(usersId);
        return Result.success(map);
    }

    /**
     * 查询用户关注的用户动态
     */
    @GetMapping("/selectUsersDynamicGet/{usersId}/{index}")
    public Result selectUsersDynamicGet(@PathVariable Integer usersId,@PathVariable Integer index){
        List<Article> sortArticleList = attentionService.selectUsersDynamic(usersId,index);
        if (sortArticleList == null){
            return Result.error("没有动态");
        }
        return Result.success(sortArticleList);
    }
    
    /**
     * 对被查询对象进行关注/取消关注行为
     * @param usersId 登录用户
     * @param followUsersId 被进行用户
     * @param attentionState 行为标识
     */
    @GetMapping("/updateAttention/{usersId}/{followUsersId}/{attentionState}")
    public Result updateAttention(@PathVariable Integer usersId,@PathVariable Integer followUsersId,@PathVariable Integer attentionState){
        attentionService.updateAttention(usersId,followUsersId,attentionState);
        return Result.success();
    }

    /**
     * 通过用户头像获取个人信息
     * @param articleUsersId 被查询对象
     * @param usersId 登录用户
     */
    @PostMapping("/getUserAttention/{articleUsersId}/{usersId}")
    public Result getUserAttention(@PathVariable Integer articleUsersId,@PathVariable Integer usersId){
        return Result.success(attentionService.selectAttention(articleUsersId,usersId));
    }
}
